package com.ultrasoft.farmasoft.util;

public class UtilTransaction {
    
    static String unitsArray [] = {"","uno","dos","tres","cuatro","cinco","seis","siete","ocho","nueve","diez","once","doce","trece","catorce","quince"};
    static String tensArray[] = {"","diez","veint","treinta","cuarenta","cincuenta","sesenta","setenta","ochenta","noventa"};
    static String hundredsArray[] = {"","cien","doscientos","trescientos","cuatrocientos","quinientos","seiscientos","setecientos","ochocientos","novecientos"};

    public static String getLiteralFormat(double numero) {
        double dollars = Math.floor(numero);
        double monto =  Math.floor(numero);
        int cents = (int)Math.round((numero*100 - dollars*100));
        double thousands = (dollars - dollars % 1000) / 1000;
        dollars -= thousands * 1000;
        String output = "";
        if ( monto >= 1000 ) {
            if ( monto < 2000 ) {
                output += "mil ";
            } else {
                output += num(thousands) + " mil ";
            }
        }
        output += dollars > 0 ? num(dollars) + "   " : "";
        output += ((cents<10)? "0"+cents : cents)  +"/100";
        return output.substring(0,1).toUpperCase() + output.substring(1);
    }

    public static String num(double k){
		int j = (int) ( (k - k % 100) / 100);
		int i = (int) (k - j * 100);
		String cen = "";
		if ( j>0 ) {
	            cen = hundredsArray[j];
	            cen += (j == 1 && i!=0) ? "to" : "";
	            cen += (i > 0) ? " " : "";
		}
		if (i<16) return cen + unitsArray[i];
		int tens =(int) ((i - i % 10) / 10);
		int units = (int) (i - (i - i % 10));
		if ( tens == 2 ) {
	            return cen + tensArray[tens] + ((units > 0) ? "i" : "e") + unitsArray[units];
		}else{
	            return cen + tensArray[tens] + ((tens > 0 && units > 0) ? " y " : "") + unitsArray[units];
		}
    }
    
    public static double rounding(double num, double exp ) {
        double base = java.lang.Math.pow( 10, exp);
        double aux = num * base;
        return (double) Math.round(aux) / base;
    }
  
    public static String round(double num, double exp ) {
        double monto = rounding(num, exp);
        String costo = "" + monto;
        String entero = "";
        String decimal = "00";
        int pos = costo.indexOf(".");
        if ( pos > 0) {
            entero = costo.substring(0,pos);
            decimal = costo.substring(pos+1)+"00";
            decimal = decimal.substring(0,2);
        } else {
            entero = costo;
        }
        String valor = entero + "." + decimal;
        return valor;
    }
}
